# Archlive <http://archlive-pkg.googlecode.com>

_kernver=$(uname -r)

pkgname=aufs2-cjk
#pkgname=aufs2
pkgver=20110815
pkgrel=1
pkgdesc="aufs2 kernel module"
#pkgdesc="aufs2 kernel module and utils aufs2内核模块及工具，kernel官方内核无ARCH内核时使用"
arch=("i686" "x86_64")
url="http://aufs.sourceforge.net"
license=('GPL')
depends=("bash" "linux-cjk")
optdepends=()
#conflicts=("aufs-utils")
provides=('aufs2-standalone')
install=aufs2.install

_gitroot="http://git.c3sl.ufpr.br/pub/scm/aufs/aufs2-standalone.git"
_gitname="aufs2-standalone"

utils_source ()
{
  cd "$srcdir"
  if [ -d aufs2-util ] ; then
    cd aufs2-util && git pull origin
    msg "The local files are updated."
  else
    git clone http://git.c3sl.ufpr.br/pub/scm/aufs/aufs2-util.git || return 1
  fi
}

utils_build ()
{
  [ -d $srcdir/aufs2-util-build ] && (rm -rf "$srcdir/aufs2-util-build")
  cp -r "$srcdir/aufs2-util" "$srcdir/aufs2-util-build"
  cd "$srcdir/aufs2-util-build"
  make || return 1
  mkdir -p "${pkgdir}"{/usr/bin,/sbin,/etc/default}
  make DESTDIR="$pkgdir/" install
}

build() {

  cd "$startdir"
  msg "Git clone aufs2-standalone tree...."
  if [ -d $_gitname ] ; then
    cd $_gitname && git pull origin
    msg "The local files are updated."
  else
    git clone $_gitroot
  fi

  #utils_source

  msg "Starting make..."

  #rev=$(echo ${_kernver#2.6.} | cut -d "-" -f1)
  rev="${_kernver:4:2}"
  rm -rf $srcdir/$_gitname
  cp -a $startdir/$_gitname $srcdir/$_gitname
  cd $srcdir/$_gitname
  git checkout origin/aufs2.1-$rev 2>/dev/null || git checkout origin/aufs2.1

  #patch -Np1 -i ../aufs2-module-2.6.36.patch
  sed -i 's|CONFIG_AUFS_BRANCH_MAX_127 =.*|CONFIG_AUFS_BRANCH_MAX_127 =|' \
     config.mk || return 1
  sed -i 's|CONFIG_AUFS_BRANCH_MAX_511 =.*|CONFIG_AUFS_BRANCH_MAX_511 = y|' \
     config.mk || return 1
  sed -i 's|CONFIG_AUFS_SBILIST =.*|CONFIG_AUFS_SBILIST = y|' \
     config.mk || return 1
  sed -i 's|CONFIG_AUFS_HNOTIFY =.*|CONFIG_AUFS_HNOTIFY = y|' \
     config.mk || return 1
  sed -i 's|CONFIG_AUFS_HFSNOTIFY =.*|CONFIG_AUFS_HFSNOTIFY = y|' \
     config.mk || return 1
  sed -i 's|CONFIG_AUFS_EXPORT =.*|CONFIG_AUFS_EXPORT = y|' \
     config.mk || return 1
  sed -i 's|CONFIG_AUFS_RDU =.*|CONFIG_AUFS_RDU = y|' \
     config.mk || return 1
  sed -i 's|CONFIG_AUFS_SP_IATTR =.*|CONFIG_AUFS_SP_IATTR = y|' \
     config.mk || return 1
  sed -i 's|CONFIG_AUFS_SHWH =.*|CONFIG_AUFS_SHWH = y|' \
     config.mk || return 1
  sed -i 's|CONFIG_AUFS_BR_RAMFS =.*|CONFIG_AUFS_BR_RAMFS = y|' \
     config.mk || return 1
  sed -i 's|CONFIG_AUFS_BR_FUSE =.*|CONFIG_AUFS_BR_FUSE = y|' \
     config.mk || return 1
  sed -i 's|CONFIG_AUFS_POLL =.*|CONFIG_AUFS_POLL = y|' \
     config.mk || return 1
  sed -i 's|CONFIG_AUFS_BR_HFSPLUS =.*|CONFIG_AUFS_BR_HFSPLUS = y|' \
     config.mk || return 1
  sed -i 's|CONFIG_AUFS_BDEV_LOOP =.*|CONFIG_AUFS_BDEV_LOOP = y|' \
     config.mk || return 1

#  sed -i 's|CONFIG_AUFS_MAGIC_SYSRQ =.*|CONFIG_AUFS_MAGIC_SYSRQ = y|' \
#     config.mk || return 1   #need CONFIG_AUFS_DEBUG

  sed -i 's|CONFIG_AUFS_DEBUG =.*|CONFIG_AUFS_DEBUG =|' \
     config.mk || return 1
  if [ "${CARCH}" = "x86_64" ]; then
    inot64=" y"
  else
    inot64=""
  fi
  sed -i "s|CONFIG_AUFS_INO_T_64 =.*|CONFIG_AUFS_INO_T_64 =${inot64}|" \
     config.mk 
  # build, sed fixes are from gentoo portage build
  sed -i "s:aufs.ko usr/include/linux/aufs_type.h:aufs.ko:g" Makefile
  sed -i "s:__user::g" include/linux/aufs_type.h
  make KDIR=/usr/src/linux-${_kernver}

  #utils_build
} 
package() {
  cd $srcdir/$_gitname
  install -D -m644 fs/aufs/aufs.ko \
    $pkgdir/lib/modules/${_kernver}/kernel/fs/aufs/aufs.ko 
  # tweak the install script for the right kernel version
  sed -i -e "s/KERNEL_VERSION=.*/KERNEL_VERSION=${_kernver}/g" \
    $startdir/aufs2.install
  # install include files
  install -D -m 644 include/linux/aufs_type.h $pkgdir/usr/include/linux/aufs_type.h
  # gzip -9 all modules to safe 100MB
  find "$pkgdir" -name '*.ko' -exec gzip -9 {} \;

  #pkgver=${_kernver/-ARCH/}_$pkgver
}
